$(function() {
    var Alert1 = new Alert();
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    var confirm1 = new Confirm();
    var index = {
        init: function() {
            this.bind();
            maxLengthLimit.init("publicText");
        },
        bind: function() {
            var self = this;
            $(".looktg").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName": "teacherDetailsOfTheExperimentReport",
                    "paramData": {
                        "chapterNum": result.paramData.chapterNum,
                        "courseId": result.paramData.courseId,
                        "classId": result.paramData.classId,
                        "chapterId": result.paramData.chapterId,
                        "some": result.paramData.some,
                        "somes": result.paramData.somes
                    }
                });

                window.location.href = "skipPage?jsonStr=" + json;
            });
            var sourceMark;
            // 获取数据
            var json = JSON.stringify({"stuorteaId":result.paramData.stuorteaId,"chapterNum":result.paramData.chapterNum,"courseId":result.paramData.courseId});
            $.ajax({
                type: "post",
                url: "getMyTestDetail",
                async: true,
                data: {
                    "jsonStr": json
                },
                success: function(msg) {
                    var msg = JSON.parse(msg);
                    if(msg.result == "success"){
                       $(".submit").attr("sum",msg.actualSumSource);
                       $("#labScore").val(msg.taskSoruce);
                        // 新增需求
                        $("#ExperimentTextarea").val(msg.lable)
                        sourceMark = msg.souceMark;
                        if(msg.taskAddress != undefined && msg.taskAddress != null && msg.taskAddress != ''){
                            $(".content").html(unescape(msg.taskAddress));
                        }else{
                            $(".content").html('<img style="margin: 20% 0 0 37%!important;" src="../images/ad_noneResource.jpg">');
                        }
                        if(sourceMark == 1){
                            $(".hide").hide();
                        }
                       if(msg.taskFraction != undefined && msg.taskFraction != null && msg.taskFraction != ''){
                            if(sourceMark == 0){
                                $(".levels").html(msg.taskFraction);
                                $(".levelTop").css("width",msg.taskSoruce/msg.actualSumSource*100+"%");
                                $(".downTitle").remove();
                                $(".content").append('<i class="downTitle" style="font-style: normal;font-size:14px;position: absolute;right: 20px;top: 32px;">实验评分: <i style="font-style: normal;color: #c80211;">'+ msg.taskSoruce +'分</i>&emsp;&emsp;&emsp;&emsp;实验评价: <i style="font-style: normal;color: #c80211;">'+ msg.taskFraction +'</i></i>');
                                $(".tipInfo").html(msg.tipsStr);
                            }else{
                                $(".downTitle").remove();
                                $(".content").append('<i class="downTitle" style="font-style: normal;font-size:14px;position: absolute;right: 20px;top: 32px;">实验评分: <i style="font-style: normal;color: #c80211;">'+ msg.taskSoruce +'分</i></i>');

                            }
                      }else{
                            if(msg.taskSoruce == 0){
                                $("#labScore").val('');
                            }
                       }
                        // 新增类型判断 实训或实验
                        if(addtesTmark == 4 || addtesTmark == 5){
                            $(".downBtn").hide()
                            $(".none").hide();
                        }else{
                            if(msg.url != undefined && msg.url != null && msg.url != ''){
                                $(".downBtn").show();
                                $(".none").hide();
                                $(".downBtn").attr("download",msg.testfileName);
                                $(".downBtn").attr("href",msg.url);
                            }else{
                                $(".downBtn").hide();
                                $(".none").show();
                            }
                        }
                    }else{
                        Alert1.content = msg.failMsg;
                        Alert1.init();
                        Alert1.show();
                    }


                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
            // 新增需求
            var ExperimentTextarea = $("#ExperimentTextarea");
            ExperimentTextarea.off("keyup").on("keyup", function () {
                ExperimentTextarea.keyup(function(){
                    if(ExperimentTextarea.val().length > 200){
                        ExperimentTextarea.val(ExperimentTextarea.val().substring(0,200) );
                    }
                    $("#ExperimentTextNumberSpan").text( 200 - ExperimentTextarea.val().length ) ;
                    return
                });
            })
            $("#labScore").off("blur").blur(function () {
                var numVal = $("#labScore").val();
                var reg = /^\d+(\.\d+)?$/;
                if(reg.test(numVal)){
                    if(sourceMark == 0){
                        var json = JSON.stringify({"taskSoruce":Number(numVal),"courseId":result.paramData.courseId});
                        $.ajax({
                            type: "post",
                            url: "getTestTaskFraction",
                            async: true,
                            data: {
                                "jsonStr": json
                            },
                            success: function (msg) {
                                var msg = JSON.parse(msg);
                                if (msg.result == "success") {
                                    $(".levels").html(msg.taskFraction);
                                    $(".levelTop").css("width",numVal/Number($(".submit").attr("sum"))*100+"%");
                                    $(".downTitle").remove();
                                    $(".content").append('<i class="downTitle" style="font-style: normal;font-size:14px;position: absolute;right: 20px;top: 32px;">实验评分: <i style="font-style: normal;color: #c80211;">'+ numVal +'分</i>&emsp;&emsp;&emsp;&emsp;实验评价: <i style="font-style: normal;color: #c80211;">'+ msg.taskFraction +'</i></i>');
                                }else{
                                    Alert1.content = msg.failMsg;
                                    Alert1.init();
                                    Alert1.show();
                                }
                            }
                        });
                    }else{
                        $(".downTitle").remove();
                        $(".content").append('<i class="downTitle" style="font-style: normal;font-size:14px;position: absolute;right: 20px;top: 32px;">实验评分: <i style="font-style: normal;color: #c80211;">'+ numVal +'分</i></i>');
                    }
                }else{
                    Alert1.content = "请正确输入实验评分";
                    $("#labScore").val('');
                    Alert1.init();
                    Alert1.show();
                }
            });
            $(".submit").off("click").click(function () {
                if($("#labScore").val() != ''){
                    // if(Number($("#labScore").val())>Number($(".submit").attr("sum"))){
                    //     Alert1.content = "实验评分大于设置的实验报告总分数，请重新输入";
                    //     Alert1.init();
                    //     Alert1.show();
                    // }else{
                        var json = JSON.stringify({
                            "chapterNum":Number(result.paramData.chapterNum),
                            "courseId":Number(result.paramData.courseId),
                            "chapterId":Number(result.paramData.chapterId),
                            "stuorteaId":result.paramData.stuorteaId,
                            "submitContent":'',
                            "taskSoruce":Number($("#labScore").val()),
                            "scoreList":'',
                            "blankscount":0,
                            "blanksallsource":0,
                            "answercount":0,
                            "answersource":0,
                            "htmlStr":$(".content").html(),
                            "cssStr":'*{margin:0;padding:0;}#contentBox{padding-top:60px;}',
                            "taskFraction":$(".levels").html(),
                            "lable": $("#ExperimentTextarea").val()
                        });
                        $.ajax({
                            type: "post",
                            url: "assessTest",
                            async: true,
                            data: {
                                "jsonStr": json
                            },
                            success: function (msg) {
                                var msg = JSON.parse(msg);
                                if (msg.result == "success") {
                                    Alert1.content = "保存成功";
                                    Alert1.success = function () {
                                      window.location.reload(true);
                                    };
                                    Alert1.init();
                                    Alert1.show();
                                    // $(".downBtn").show();
                                    // $(".none").hide();
                                    // $(".downBtn").attr("download",msg.testfileName);
                                    // $(".downBtn").attr("href",msg.url);
                                }else{
                                    Alert1.content = msg.failMsg;
                                    Alert1.init();
                                    Alert1.show();
                                }
                            }
                        });
                    // }
                }else{
                    Alert1.content = "请输入实验评分";
                    Alert1.init();
                    Alert1.show();
                }

            });
        }
    };
    index.init();

});